package com.woniuxy.jiapei.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.jiapei.mapper.RoleMapper;
import com.woniuxy.jiapei.mapper.ShareholderMapper;
import com.woniuxy.jiapei.mapper.UserRoleMapper;
import com.woniuxy.jiapei.pojo.Role;
import com.woniuxy.jiapei.pojo.Shareholder;
import com.woniuxy.jiapei.pojo.User;
import com.woniuxy.jiapei.mapper.UserMapper;
import com.woniuxy.jiapei.pojo.UserRole;
import com.woniuxy.jiapei.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.jiapei.vo.ShareholdersQueryVo;
import com.woniuxy.jiapei.vo.ShareholdersVo;
import org.springframework.beans.NotWritablePropertyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lr
 * @since 2020-10-21
 */
@Transactional
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShareholderMapper shareholderMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public IPage<ShareholdersVo> selectShareholderList(Page<ShareholdersVo> page, ShareholdersQueryVo vo) {
        QueryWrapper<ShareholdersVo> wrapper = new QueryWrapper<>();
        if (vo.getQueryType() == 1) {
            if (!StrUtil.isEmpty(vo.getStartDate())) {
                wrapper.ge("createtime", vo.getStartDate());
            }
            if (!StrUtil.isEmpty(vo.getEndDate())) {
                wrapper.le("createtime", vo.getEndDate());
            }
        } else {
            String userName = vo.getUserName();
            if (!StrUtil.isEmpty(userName)) {
                try {
                    Integer.parseInt(userName);
                    wrapper.like("phone", userName);
                } catch (NumberFormatException e) {
                    wrapper.like("name", userName);
                }
            }
        }
        return userMapper.selectShareholderList(page, wrapper);
    }

    @Override
    public List<ShareholdersVo> selectShareholderListExcel(ShareholdersQueryVo vo) {
        QueryWrapper<ShareholdersVo> wrapper = new QueryWrapper<>();
        if (!StrUtil.isEmpty(vo.getStartDate())) {
            wrapper.ge("createtime", vo.getStartDate());
        }
        if (!StrUtil.isEmpty(vo.getEndDate())) {
            wrapper.le("createtime", vo.getEndDate());
        }
        return userMapper.selectShareholderListExcel(wrapper);
    }

    @Override
    public void addShareholder(ShareholdersVo vo) {
        User user = new User(null, vo.getName(), vo.getWxName(), vo.getPhone(), vo.getEmail(), DigestUtil.md5Hex(vo.getPassword()), new Date());
        userMapper.insert(user);
        Shareholder shareholder = new Shareholder(null, user.getId(), vo.getInvestmentamount(), vo.getShare(), vo.getEntrytime(), vo.getCardnumber());
        shareholderMapper.insert(shareholder);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("name","股东");
        Role role = roleMapper.selectOne(wrapper);
        UserRole userRole = new UserRole(user.getId(), role.getId());
        System.out.println(userRole);
        userRoleMapper.insert(userRole);
    }

    @Override
    public void deleteShareholder(Integer id) {
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        userRoleMapper.delete(wrapper);
        QueryWrapper<Shareholder> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("use_id",id);
        shareholderMapper.delete(wrapper1);
        userMapper.deleteById(id);
    }

    @Override
    public void updateShareholder(ShareholdersVo vo) {
        User user = new User();
        user.setId(vo.getId());
        user.setName(vo.getName());
        user.setPhone(vo.getPhone());
        user.setWxName(vo.getWxName());
        user.setEmail(vo.getEmail());
        user.setCreatetime(vo.getCreatetime());
        userMapper.updateById(user);
        Shareholder shareholder = new Shareholder();
        shareholder.setUseId(vo.getId());
        shareholder.setInvestmentamount(vo.getInvestmentamount());
        shareholder.setShare(vo.getShare());
        shareholder.setCardnumber(vo.getCardnumber());
        shareholder.setEntrytime(vo.getEntrytime());
        QueryWrapper<Shareholder> wrapper = new QueryWrapper<>();
        wrapper.eq("use_id",vo.getId());
        shareholderMapper.update(shareholder,wrapper);
    }
}
